#include "view_prompt.h"
#include "prj_typedef.h"
#include "blecom.h"
#include "display_ctrl.h"
#include "view_abstract.h"
#include "platform_assert.h"
#include "app_trace.h"
#include "motor.h"
#include "prj_config.h"
#include "display_interface.h"
#include "lfs_file_manager_auto.h"
#include "stopwatch.h"
#include "heart_rate_interface.h"
#include "rtc.h"
#include "app_trace.h"
#include "m_product_test.h"
#include "battery_ctrl.h"
#include "view_prompt.inc"
#include "view_prompt_message.h"
#include "Motor.h"
#include "font/font.h"
#include "sport_his.h"
#include "math.h"
#include "weather_ctrl.h"
#if defined(USE_CT1711)
#include "body_temperature.h"
#endif
// #define P_REMIND_X_POS 17
// #define P_REMIND_Y_POS 42

// #define L_REMIND_X_POS 45
// #define L_REMIND_Y_POS 14

void view_prompt_not_wear_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;
        if (disp_ctrl.auto_refresh_count >= 5 * 2)
        {
            display_show_type_recover();
        }
    }
    draw_on_chip_unzip_gradients_image_with_back_ground(0, 0, NULL, RGB565_COLOR_WHITE, 0xfc8d, 0xfa43);
    draw_on_chip_gray_gif_array_filter_color(16, 38, zip_gif_not_wear, sizeof(zip_gif_not_wear) / sizeof(zip_gif_not_wear[0]), RGB565_COLOR_WHITE);
}
void view_prompt_lost_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        if (disp_ctrl.auto_refresh_count >= 5 * 2)
        {
            display_show_type_recover();
        }
    }
    draw_on_chip_unzip_gradients_image_with_back_ground(0, 0, NULL, RGB565_COLOR_WHITE, 0xf649, 0xf4c9);
    draw_on_chip_image_ext(20, 8, zip_image_band, draw_type_filter_color);
    draw_on_chip_gif_array_filter_color(20, 58, zip_gif_link, sizeof(zip_gif_link) / sizeof(zip_gif_link[0]));
    draw_on_chip_image_ext(20, 92, zip_image_phone_with_ble, draw_type_filter_color);
}
void view_prompt_drink_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;
        if (disp_ctrl.auto_refresh_count >= 5 * 2)
        {
            display_show_type_recover();
        }
    }
    draw_on_chip_unzip_gradients_image_with_back_ground(0, 0, NULL, RGB565_COLOR_WHITE, 0x56bf, 0x44bf);
    draw_on_chip_gif_array_filter_color(17, 52, zip_gif_drink, sizeof(zip_gif_drink) / sizeof(zip_gif_drink[0]));
}

#if defined(USE_CT1711)
void view_prompt_body_temperature_fail_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        if (disp_ctrl.auto_refresh_count == 0)
        {
            motor_start(200);
        }
        disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;
        if (disp_ctrl.auto_refresh_count >= 5)
        {
            display_show_type_recover();
        }
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
    }
//  #warning debug 
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 56, 31, FILE_IMG_t509_tem_fail_tips_56_31);
    // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_MeasuringBodyTemperatureFailTips, 50);
}
void view_prompt_hyperthemia_display(void)
{ 
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.f_temp[0] = get_body_temperature();
       // disp_ctrl.com_data.f_temp[0]  = 39.4f;
        if (isnan(disp_ctrl.com_data.f_temp[0]))
        {
            display_show_type_recover();
            return;
        }
        disp_ctrl.com_data.i_temp[21] = weather_get_temperature_type();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
    }
    // if (disp_ctrl.com_data.i_temp[5])
    // {
    //     disp_draw_user_image(42, 3, img_ble_connect);
    // }
    // else
    // {
    //     disp_draw_user_image(42, 0, img_ble_disconnect);
    // }
    // show_common_time(disp_ctrl.com_data.i_temp[3], disp_ctrl.com_data.i_temp[4]);
    // show_time_0_battery(disp_ctrl.com_data.i_temp[9], disp_ctrl.com_data.i_temp[8]);
    float temperature = disp_ctrl.com_data.f_temp[0];
    uint16_t back_color = 0;
    int back_start_y = 0;
    int back_end_y = 0;
    int y = 0;
    if (temperature >= 32.f && temperature < 37.5f)
    {
        back_start_y = 0;
        back_end_y = OLED_SIZE_Y / 2 - 1;
        back_color = 0x1fc5;
        y = 40;
        if (disp_ctrl.loop_cnt == 0)
        {
            if (disp_ctrl.auto_refresh_count >= 5)
            {
                if (disp_ctrl.disp_type_back != view_temperature_icon)
                {
                    display_show(view_close_oled, DISP_MODE_NORMAL);
                }
                else
                {
                    display_show_type_recover();
                }
            }
        }
    }
    else if (temperature >= 37.5f && temperature < 38.5f)
    {
        back_start_y = OLED_SIZE_Y / 2;
        back_end_y = OLED_SIZE_Y - 1;
        back_color = 0xfa23;
        y = 111;
        if (disp_ctrl.loop_cnt == 0)
        {
            if (disp_ctrl.auto_refresh_count < 3)
            {
                motor_start(100);
            }
            if (disp_ctrl.auto_refresh_count >= 10)
            {
                if (disp_ctrl.disp_type_back != view_temperature_icon)
                {
                    display_show(view_close_oled, DISP_MODE_NORMAL);
                }
                else
                {
                    display_show_type_recover();
                }
            }
        }
    }
    else if (temperature >= 38.5f && temperature < 43.f)
    {
        back_start_y = 0;
        back_end_y = OLED_SIZE_Y - 1;
        back_color = RGB565_COLOR_RED;
        y = 71;
        if (disp_ctrl.loop_cnt == 0)
        {
            if (disp_ctrl.auto_refresh_count < 5)
            {
                motor_start(100);
            }
            if (disp_ctrl.auto_refresh_count >= 30)
            {
                if (disp_ctrl.disp_type_back != view_temperature_icon)
                {
                    display_show(view_close_oled, DISP_MODE_NORMAL);
                }
                else
                {
                    display_show_type_recover();
                }
            }
        }
    }
    else
    {
        return;
    }
    draw_rectangle(0, back_start_y, OLED_SIZE_X, back_end_y, back_color);
    bool is_centigrade = disp_ctrl.com_data.i_temp[21] == WEATHER_TEMPERATURE_TYPE_C;
    if (!is_centigrade)
    {
        temperature = 32.f + temperature * 1.8f;
    }
    int temperature_int = temperature * 100;

    int const font_width = 8;
    int const font_height = 19;
    int const font_interval = 1;
    int const point_front_interval = 2;
    int const point_width = 3;
    int const point_height = 3;
    int const point_back_interval = 2;
    int const uinit_interval = 2;
    int const unit_widht = 14;
    int num_digits = get_num_digit(temperature_int / 10);
    int const total_width = font_width * num_digits + (num_digits - 2) * font_interval + point_front_interval + point_width + point_back_interval + uinit_interval + unit_widht;
    int draw_x = (OLED_SIZE_X - 1 - total_width) / 2;
//    if (disp_ctrl.loop_cnt == 0)
//    {
//        LOG("draw_x %d", draw_x);
//    }
    // num_8x19_TAB
    // 32 - 37.4 normal
    // 37.5 -38.4 yellow
    // red 38.5-42.9

    if (temperature < 0)
    {
        draw_rectangle(draw_x, y + font_height / 2 - 1, draw_x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        draw_x += font_width + font_interval;
    }
    // draw_x = disp_draw_user_image_num_multiply(draw_x, y, temperature_int / 100, font_width + font_interval, num_8x19_TAB);
    draw_x = draw_on_chip_image_num(temperature_int / 100, draw_x, y, num_8x19_TAB, font_width, font_interval, draw_type_multiply);
    // draw_x -= font_interval;
    draw_x += point_front_interval;
    draw_rectangle(draw_x, y + font_height - point_height, draw_x + point_width - 1, y + font_height - 1, RGB565_COLOR_BLACK);
    draw_x += point_width + point_back_interval;
    // draw_x = disp_draw_user_image_num_with_digits_multiply(draw_x, y, temperature_int % 100, 2, font_width + font_interval, num_8x19_TAB);
    draw_x = draw_on_chip_image_num_with_digits(temperature_int % 100, 2, draw_x, y, num_8x19_TAB, font_width, font_interval, draw_type_multiply);

    // draw_x -= font_interval;
    draw_x += uinit_interval;

    if (is_centigrade)
    {
        draw_on_chip_image_ext(draw_x, y, num_8x19_celsius, draw_type_multiply);
    }
    else
    {
        draw_on_chip_image_ext(draw_x, y, num_8x19_fahrenheit, draw_type_multiply);
    }
//    if (disp_ctrl.loop_cnt == 0)
//    {
//        LOG("draw_x end %d", draw_x);
//    }
}
#endif
void view_prompt_alarm_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;
        if (disp_ctrl.auto_refresh_count >= 30 * 2)
        {
            display_show_type_recover();
        }
        disp_ctrl.com_data.i_temp[2] = get_present_day_minute();
        disp_ctrl.com_data.i_temp[0] = disp_ctrl.com_data.i_temp[2] / 60;
        disp_ctrl.com_data.i_temp[1] = disp_ctrl.com_data.i_temp[2] % 60;
    }
    draw_on_chip_unzip_gradients_image_with_back_ground(0, 0, NULL, RGB565_COLOR_WHITE, 0xfaed, 0xfa44);
    draw_on_chip_gif_array_filter_color(5, 35, zip_gip_alarm_clock, sizeof(zip_gip_alarm_clock) / sizeof(zip_gip_alarm_clock[0]));
}
void view_prompt_sedentariness_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;
        if (disp_ctrl.auto_refresh_count >= 5 * 2)
        {
            display_show_type_recover();
        }
    }
    draw_on_chip_unzip_gradients_image_with_back_ground(0, 0, NULL, RGB565_COLOR_WHITE, 0xaf0c, 0x5545);
    draw_on_chip_gif_array_filter_color(16, 33, zip_gip_sit, sizeof(zip_gip_sit) / sizeof(zip_gip_sit[0]));
}
void view_prompt_low_power_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;
        if (disp_ctrl.auto_refresh_count >= 5 * 2)
        {
            display_show_type_recover();
        }
    }
    draw_on_chip_unzip_gradients_image(23, 46, g_box, 0xfa69, 0xf965, RGB565_COLOR_BLACK);
    draw_on_chip_unzip_gradients_image(26, 54, g_level_warning, 0xfa69, 0xf965, RGB565_COLOR_BLACK);
}
void view_prompt_charing_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = battery_ctrl_get_percent();
        product_test_state_t state = product_test_state_get();
        switch (state)
        {
        case PRODUCT_TEST_STATE_TEST_PCBA:
            disp_ctrl.auto_refresh_time = 500;
            disp_ctrl.auto_close_delay = 2000;
            break;
        case PRODUCT_TEST_STATE_TEST_ASSEM:
            disp_ctrl.auto_refresh_time = 500;
            break;
        }
    }
    int batt_level = 0;
#if 0
#warning debug
#else
    batt_level = disp_ctrl.com_data.i_temp[0];
#endif
    draw_on_chip_gray_image(6, 5, zip_image_charge_circle, 0x2ffc);
    int offset = 0;
    // 数值
    if (batt_level >= 100)
    {
        offset = 18;
        draw_on_chip_image(offset, 22, zip_curve_num_10x28[1]);
        offset += 12;
        draw_on_chip_image(offset, 22, zip_curve_num_10x28[0]);
        offset += 12;
        draw_on_chip_image(offset, 22, zip_curve_num_10x28[0]);
        offset += 12;
        draw_on_chip_image(offset, 22, zip_image_percent);
    }
    else if (batt_level >= 10)
    {
        offset = 25;
        draw_on_chip_image(offset, 22, zip_curve_num_10x28[batt_level / 10 % 10]);
        offset += 12;
        draw_on_chip_image(offset, 22, zip_curve_num_10x28[batt_level % 10]);
        offset += 12;
        draw_on_chip_image(offset, 22, zip_image_percent);
    }
    else
    {
        offset = 29;
        draw_on_chip_image(offset, 22, zip_curve_num_10x28[batt_level % 10]);
        offset += 12;
        draw_on_chip_image(offset, 22, zip_image_percent);
    }

    draw_on_chip_gray_gif_array(22, 70, zip_image_charge_ball, sizeof(zip_image_charge_ball) / sizeof(zip_image_charge_ball[0]), 0x2ff5);
}
void view_prompt_finding_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;
        if (disp_ctrl.auto_refresh_count >= 5 * 2)
        {
            display_show_type_recover();
        }
    }

    draw_on_chip_unzip_gradients_image_with_back_ground(0, 0, NULL, RGB565_COLOR_WHITE, 0xfdca, 0xfb65);
    draw_on_chip_image_ext(20, 8, zip_image_band, draw_type_filter_color);
    draw_on_chip_gif_array_filter_color(20, 64, zip_gif_wave, sizeof(zip_gif_wave) / sizeof(zip_gif_wave[0]));
    draw_on_chip_image_ext(20, 92, zip_image_phone, draw_type_filter_color);
}
void view_prompt_steps_complete_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;
        if (disp_ctrl.auto_refresh_count >= 5)
        {
            display_show_type_recover();
            return;
        }
        disp_ctrl.com_data.i_temp[0] = sport_get_present_step();
    }
    draw_on_chip_image(0, 22, image_steps_complete);
    draw_on_chip_image_num_center(disp_ctrl.com_data.i_temp[0], 0, 108, OLED_SIZE_X - 1, 108 + 13, num_10x14_black_whiteTAB, 10, 1, draw_type_normal);
}
#if defined(USE_CT1711)
bool view_prompt_hyperthemia_event(int32_t view, event_param_t param)
{
    switch (param.event)
    {
    case BUTTON_EVENT_SHORT_PRESS:
        display_show_type_recover();
        return true;
    }
    return false;
}
#endif
void view_prompt_calories_complete_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;
        if (disp_ctrl.auto_refresh_count >= 5)
        {
            display_show_type_recover();
            return;
        }
        disp_ctrl.com_data.i_temp[0] = sport_get_calorie();
    }
    draw_on_chip_image(0, 22, image_calories_complete);
    draw_on_chip_image_num_center(disp_ctrl.com_data.i_temp[0], 0, 108, OLED_SIZE_X - 1, 108 + 13, num_10x14_black_whiteTAB, 10, 1, draw_type_normal);
}
void view_prompt_distance_complete_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;
        if (disp_ctrl.auto_refresh_count >= 5)
        {
            display_show_type_recover();
            return;
        }

        disp_ctrl.com_data.i_temp[0] = sport_get_distance();
        if (system_config_get_km_mi() != 0)
        {
            disp_ctrl.com_data.i_temp[0] *= 0.6213712;
        }
    }
    draw_on_chip_image(0, 22, image_distances_complete);
    draw_on_chip_image_num_center(disp_ctrl.com_data.i_temp[0], 0, 108, OLED_SIZE_X - 1, 108 + 13, num_10x14_black_whiteTAB, 10, 1, draw_type_normal);
}
bool view_prompt_display(int view)
{
    switch (view)
    {
    case view_prompt_message:
        view_prompt_message_display();
        return true;
    case view_prompt_distance_complete:
        view_prompt_distance_complete_display();
        return true;
    case view_prompt_calories_complete:
        view_prompt_calories_complete_display();
        return true;
    case view_prompt_steps_complete:
        view_prompt_steps_complete_display();
        return true;
    case view_prompt_lost:
        view_prompt_lost_display();
        return true;
    case view_prompt_drink:
        view_prompt_drink_display();
        return true;
    case view_prompt_alarm:
        view_prompt_alarm_display();
        return true;
    case view_prompt_sedentariness:
        view_prompt_sedentariness_display();
        return true;
    case view_prompt_low_power:
        view_prompt_low_power_display();
        return true;
    case view_prompt_charing:
        view_prompt_charing_display();
        return true;
    case view_prompt_finding:
        view_prompt_finding_display();
        return true;
    case view_prompt_not_wear:
        view_prompt_not_wear_display();
        return true;
        // case view_prompt_camera_ready:
        //     view_prompt_camera_ready_display();
        //     return true;
        // case view_prompt_camera_fail:
        //     view_prompt_camera_fail_display();
        //     return true;
#if defined(USE_CT1711) 
    case view_prompt_hyperthemia:
        view_prompt_hyperthemia_display();
        return true;
    case view_prompt_body_temperature_fail:
        view_prompt_body_temperature_fail_display();
        return true;
#endif
    }
    return false;
}
bool view_prompt_alarm_event(int32_t view, event_param_t param)
{
    switch (param.event)
    {
    case event_view_exit:
        motor_stop();
        return true;
    }
    return false;
}
bool view_prompt_find_wrist_event(int32_t view, event_param_t param)
{
    switch (param.event)
    {
    case event_view_exit:
    {
        motor_stop();
        return true;
    }
    }
    return false;
}

// bool view_prompt_distance_complete_event(int32_t view, event_param_t param)
//{
//    switch (param.event)
//    {
//    case event_view_entry:
//        return true;
//    }
//    return false;
//}

// bool view_prompt_calories_complete_event(int32_t view, event_param_t param)
//{
//    switch (param.event)
//    {
//    case event_view_entry:
//        return true;
//    }
//    return false;
//}
// bool view_prompt_steps_complete_event(int32_t view, event_param_t param)
//{
//    switch (param.event)
//    {
//    case event_view_entry:
//        return true;
//    }
//    return false;
//}
bool view_prompt_event(int view, event_param_t param)
{
    if (view < view_prompt_start || view >= view_prompt_end)
    {
        return false;
    }
    bool result = false;
    switch (view)
    {
    case view_prompt_message:
        result = view_prompt_message_event(view, param);
        break;
    case view_prompt_alarm:
        result = view_prompt_alarm_event(view, param);
        break;
        //    case view_prompt_distance_complete:
        //        result = view_prompt_distance_complete_event(view, param);
        //        break;
        //    case view_prompt_calories_complete:
        //        result = view_prompt_calories_complete_event(view, param);
        //        break;
        //    case view_prompt_steps_complete:
        //        result = view_prompt_steps_complete_event(view, param);
        //        break;
    case view_prompt_finding:
        result = view_prompt_find_wrist_event(view, param);
        break;
#if defined(USE_CT1711)
    case view_prompt_hyperthemia:
        result = view_prompt_hyperthemia_event(view, param);
        break;
#endif
    }
    if (result)
    {
        return true;
    }
    switch (param.event)
    {
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
        display_show_type_recover();
        return true;
    }
    return false;
}
bool view_power_off_charing_event(event_param_t param)
{
    if (!battery_ctrl_if_charging())
    {
        return false;
    }
    switch (param.event)
    {
    case BUTTON_EVENT_SHORT_PRESS:
        if (disp_ctrl.auto_close_delay < DISP_CLOSE_TIME_DEFAULT)
        {
            disp_ctrl.auto_close_delay = DISP_CLOSE_TIME_DEFAULT;
        }
        display_show(view_prompt_charing, DISP_MODE_NORMAL);
        break;
    }
    return false;
}
